

package com.platform.server.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.paltform.server.entity.ExcelIdentity;
import com.paltform.server.entity.SysUser;
import com.platform.server.service.IdentityService;
import com.platform.server.util.ExportExcel;
import com.szrca.ima.utils.StringTools;

@Controller
public class IdentityController {

	@Autowired
	private IdentityService identityService;
	
	/**
	 * 身份审核列表
	 */
	@RequestMapping("/selectIdentityList")
	public Object selectIdentityList(HttpServletRequest request,String userid,String username,String startTime,String endTime,String status,String mobile,String addres,String a){
		SimpleDateFormat fdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if("请选择分类".equals(status)){
			status = null;
		}
		List<Map<String, Object>> lists = identityService.selectIdentityList(userid, username, startTime, endTime,status,mobile,addres);
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("create_time",fdt.format(lists.get(i).get("create_time")));   
		}
		request.setAttribute("lists", lists);
		request.setAttribute("nums", lists.size());
		if(!StringTools.isEmpty(userid)){
			if(!StringTools.isEmpty(a)){
				request.setAttribute("a", a);
			}
			return "/ui/identity/identity_info.jsp";
		}
		return "/ui/identity/identity_list.jsp";
	}
	/**
	 * 修改身份认证状态
	 * @throws IOException 
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/batchIdentityStatus")
	public void batchIdentityStatus(HttpSession session,HttpServletResponse response,String audit,String user_id) throws Exception{
		String[] split = user_id.split(",");
		SysUser su = (SysUser) session.getAttribute("user");
		int flag=0;
		for (int i = 0; i < split.length; i++) {
			List<Map<String, Object>> lists = identityService.selectIdentityList(split[i], null, null, null,null,null,null);
			if(!lists.get(0).get("audit").toString().equals("1")){
				flag=identityService.updateIdentityStatus(audit, su.getSys_name(),split[i]);
			}
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	/**
	 * 修改身份认证状态
	 */
	@RequestMapping("/updateIdentityStatus")
	public Object updateIdentityStatus(HttpSession session,String audit,String user_id){
		SysUser su = (SysUser) session.getAttribute("user");
		int flag = identityService.updateIdentityStatus(audit, su.getSys_name(),user_id);
		System.out.println(flag);
		return "/selectIdentityList";
	}
	// 导出报表
	@RequestMapping("/identityExportExcel")
	public void identityExportExcel(HttpServletResponse response,String userid,String username,String startTime,String endTime,String status,String mobile,String addres) {
		List<Object> list2 = new ArrayList<Object>();
		List<Map<String, Object>> lists = identityService.selectIdentityList(userid, username, startTime, endTime,status,mobile,addres);
		for (Map<String, Object> map : lists) {
			ExcelIdentity excelIdentity = new ExcelIdentity();
			excelIdentity.setCreate_time(map.get("create_time"));
			excelIdentity.setIdentity_card(map.get("identity_card"));
			if(map.get("audit").toString().equals("0")){
				excelIdentity.setAudit("未认证");
			}
			if(map.get("audit").toString().equals("1")){
				excelIdentity.setAudit("已认证");
			}
			if(map.get("audit").toString().equals("2")){
				excelIdentity.setAudit("未认证");
			}
			if(map.get("face_statu")!=null&&map.get("face_statu").toString().equals("1")){
				excelIdentity.setFace_statu("已认证");
			}	
			if(map.get("face_statu")!=null&&map.get("face_statu").toString().equals("0")){
				excelIdentity.setFace_statu("未认证");
			}
			excelIdentity.setMobile(map.get("mobile"));
			excelIdentity.setRecommend_mobile(map.get("recommend_mobile"));
			excelIdentity.setUsername(map.get("username"));
		list2.add(excelIdentity);
		}
		String[] titles = { "姓名", "手机号", "身份证", "推荐人手机号", "实名认证", "人脸识别", "提交时间"};
		ExportExcel.exportExcel("身份审核列表.xls", titles, list2, response);
	}
}
